//
//  XJ_SelectFeiLvView.m
//  XunJieProject
//
//  Created by xue on 2018/6/20.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_SelectFeiLvView.h"
#import "XJ_SelectFeiLvCell.h"

@interface XJ_SelectFeiLvView()<UITableViewDelegate,UITableViewDataSource>
// 背景视图
@property (nonatomic, strong) UIView *backgroundView;
//弹窗
@property (nonatomic,strong) UIView *alertView;

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSString *collectMoney;
@end
@implementation XJ_SelectFeiLvView

- (instancetype)initWithTitle:(NSString*)title array:(NSMutableArray*)dataArr momey:(NSString*)money{
    
    if (self == [super init]) {
        
        self.collectMoney =money;
        self.dataArray = [NSMutableArray arrayWithArray:dataArr];
        self.frame = [UIScreen mainScreen].bounds;
        [self addSubview:self.backgroundView];
        
        self.alertView = [[UIView alloc] init];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.layer.cornerRadius = 2.0;
        self.alertView.clipsToBounds =YES;
        self.alertView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 240);
        
        
        UILabel *titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        titleLabel.backgroundColor =ClearColor;
        titleLabel.text =title;
        titleLabel.textAlignment =NSTextAlignmentCenter;
        titleLabel.font =[UIFont systemFontOfSize:17.0];
        [self.alertView addSubview:titleLabel];
        
        
        UIButton *backBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.backgroundColor =ClearColor;
        backBtn.frame =CGRectMake(0, 6, 44, 44);
        [backBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
        [self.alertView addSubview:backBtn];
        [backBtn addTarget:self action:@selector(Click_hidden) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *linView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(backBtn.frame), SCREEN_WIDTH, 1)];
        linView.backgroundColor =Color_Line;
        [self.alertView addSubview:linView];
        
        
        [self.alertView addSubview:self.tableView];
        
        [self addSubview:self.alertView];
        
    }
    return self;
}
#pragma mark - 弹出 -
- (void)showXLAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}
-(void)Click_hidden{
   
    
    // 关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y += 230;
        self.alertView.frame = rect;
        self.backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        
        [self.alertView removeFromSuperview];
        [self.backgroundView removeFromSuperview];
        [self removeFromSuperview];
        
        self.alertView = nil;
        self.backgroundView = nil;
    }];
}

-(UITableView*)tableView{
    
    if (!_tableView) {
        _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, CGRectGetHeight(self.alertView.frame)-60) style:UITableViewStylePlain];
        _tableView.backgroundColor =ClearColor;
        _tableView.delegate =self;
        _tableView.dataSource =self;
        _tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
        
    }
    return _tableView;
}
#pragma mark - 背景遮罩图层
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _backgroundView.userInteractionEnabled = YES;
//        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
//        [_backgroundView addGestureRecognizer:myTap];
    }
    return _backgroundView;
}
//#pragma mark - 背景视图的点击事件
//- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
//    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
//        self.transform = CGAffineTransformMakeScale(0.0, 0.0);
//    } completion:^(BOOL finished) {
//        [self removeFromSuperview];
//    }];
//}
- (void)creatShowAnimation
{
    
    // 动画前初始位置
    CGRect rect = self.alertView.frame;
    rect.origin.y = SCREEN_HEIGHT;
    self.alertView.frame = rect;
    
    // 浮现动画
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y -= 230;
        self.alertView.frame = rect;
    }];
}

#pragma mark--UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * identifier = @"cell";
    XJ_SelectFeiLvCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        
        cell =[[[NSBundle mainBundle] loadNibNamed:@"XJ_SelectFeiLvCell" owner:nil options:nil]firstObject];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        cell.backgroundColor =[UIColor clearColor];
        
    }
    cell.titleLabel.textColor =RGBCOLOR(152, 153, 154);
    cell.selectBtn.tag =indexPath.row;
    
    
    XJ_CollectPlansModel *CollectPlans_model =_dataArray[indexPath.row];
    if (CollectPlans_model.isSelect) {
        cell.selectBtn.hidden =NO;
    }else{
        cell.selectBtn.hidden =YES;
    }
    
    CGFloat rate =[CollectPlans_model.rate floatValue];
    CGFloat cost =[CollectPlans_model.cost floatValue];
    CGFloat money =[self.collectMoney floatValue];
    CGFloat collectPlans =rate/100*money+cost;
    if (money==0) {
        collectPlans =0.0;
    }
    NSString *collectPlansStr =[NSString stringWithFormat:@"%.1f",collectPlans];
    cell.titleLabel.text =[NSString stringWithFormat:@"费率:%@%@+%@元，本次手续费:%@元",CollectPlans_model.rate,@"%",CollectPlans_model.cost,collectPlansStr];
    
    if (indexPath.row==_dataArray.count-1) {
        cell.lineView.hidden =YES;
    }
    return cell;
   
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XJ_SelectFeiLvCell *cell =(XJ_SelectFeiLvCell *)[self.tableView  cellForRowAtIndexPath:indexPath];
    for (XJ_CollectPlansModel *model in _dataArray) {
        model.isSelect =NO;
    }
    XJ_CollectPlansModel *CollectPlans_model =_dataArray[indexPath.row];
    CollectPlans_model.isSelect =YES;
    
    if (self.block) {
        self.block(CollectPlans_model, cell.titleLabel.text);
    }
    [self Click_hidden];
}


@end
